<?php

namespace app\store\model;

use app\common\model\Team as TeamModel;

/**
 * 文章模型
 * Class Article
 * @package app\store\model
 */
class Team extends TeamModel
{
    /**
     * 获取文章列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        return $this->with(['image', 'category', 'banner','header'])
            ->where('is_delete', '=', 0)
            ->order(['team_sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);

    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        if (empty($data['banner_id'])) {
            $this->error = '请上传服务队banner图';
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        // 开启事务
        $this->startTrans();
        try {
            $this->allowField(true)->save($data);
            // 相册图片
            if (isset($data['images'])){
                $this->addTeamImages($data['images']);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 添加服务队相册图片
     * @param $images
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function addTeamImages($images)
    {
        $this->images()->delete();
        $data = array_map(function ($image_id) {
            return [
                'image_id' => $image_id,
                'wxapp_id' => self::$wxapp_id
            ];
        }, $images);
        return $this->images()->saveAll($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        if (empty($data['banner_id'])) {
            $this->error = '请上传服务队banner图';
            return false;
        }
//        if (empty($data['team_content'])) {
//            $this->error = '请输服务队简介';
//            return false;
//        }
//        if (!isset($data['images']) || empty($data['images'])) {
//            $this->error = '服务队相册至少需要一张图片';
//            return false;
//        }
        return $this->transaction(function () use ($data) {
            $this->allowField(true)->save($data);
            if (isset($data['images'])){
                $this->addTeamImages($data['images']);
            }

            return true;
        });
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 获取文章总数量
     * @param array $where
     * @return int|string
     */
    public static function getArticleTotal($where = [])
    {
        $model = new static;
        !empty($where) && $model->where($where);
        return $model->where('is_delete', '=', 0)->count();
    }

}